package com.example.springboot.oss.controller;

import com.qcloud.cos.model.COSObjectSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.oss.service.CosService;
import java.net.URL;
import java.util.List;

@RestController
@RequestMapping("/api/cos")
public class CosController {

    private final CosService cosService;

    @Autowired
    public CosController(CosService cosService) {
        this.cosService = cosService;
    }

    @PostMapping("/upload")
    public String uploadObject(@RequestParam String key, @RequestParam String filePath) {
        cosService.uploadObject("shipin-1330439758", key, filePath);
        return "File uploaded successfully";
    }

    @DeleteMapping("/delete")
    public String deleteObject(@RequestParam String key) {
        cosService.deleteObject("shipin-1330439758", key);
        return "File deleted successfully";
    }

    @GetMapping("/list")
    public List<COSObjectSummary> listObjects() {
        return cosService.listObjects("shipin-1330439758");
    }

    @PutMapping("/update")
    public String updateObject(@RequestParam String key, @RequestParam String newFilePath) {
        cosService.updateObject("shipin-1330439758", key, newFilePath);
        return "File updated successfully";
    }

    @GetMapping("/download")
    public String downloadObject(@RequestParam String key, @RequestParam String downloadPath) {
        cosService.getObject("shipin-1330439758", key, downloadPath);
        return "File downloaded successfully";
    }

    @GetMapping("/play")
    public URL playObject(@RequestParam String key) {
        URL url = cosService.generatePresignedUrl("shipin-1330439758", key);
        return url;
    }
}